package top.dancun.easteacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.dancun.common.entity.RoleEntity;
import top.dancun.common.entity.UserEntity;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.Query;
import top.dancun.easteacher.dao.TeacherDao;
import top.dancun.easteacher.entity.TeacherEntity;
import top.dancun.easteacher.entity.UserRoleEntity;
import top.dancun.easteacher.feign.service.SysFeignService;
import top.dancun.easteacher.service.TeacherService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity> implements TeacherService {
    @Autowired
    private SysFeignService sysFeignService;
    @Override
    public boolean save(TeacherEntity teacher) {
        UserEntity user = new UserEntity();
        user.setUid(teacher.getTid());
        user.setStatus(0);
        user.setPwd(teacher.getTid());
        //添加教师角色
        RoleEntity role = new RoleEntity();
        role.setRoleId(3l);
        List<RoleEntity> roleList = new ArrayList<>();
        roleList.add(role);
        user.setRoles(roleList);
        sysFeignService.saveUser(user);

        return super.save(teacher);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TeacherEntity> page = this.page(
                new Query<TeacherEntity>().getPage(params),
                new QueryWrapper<TeacherEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<TeacherEntity> search(Map<String, String> params) {
        String name  = params.get("name");
        String tid = params.get("tid");
        StringUtils.isNotBlank(tid);
        return  lambdaQuery()
                .like(StringUtils.isNotBlank(name)
                        ,TeacherEntity::getName,
                        name)
                .like(StringUtils.isNotBlank(tid)
                        ,TeacherEntity::getTid
                        ,tid
                )
                .list();
    }

}